﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Entity;
using PM.Services;

namespace PM.Site.Admin
{
    public partial class DividendReceivables : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 1)
                {
                    this.Loading();
                }
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }

            }
            else
            {
                Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
            }

        }

        private void Loading()
        {

            if (!Page.IsPostBack)
            {
                ddlClientIds.DataSource = ClientServices.GetClientID_Name_Collection();
                ddlClientIds.DataTextField = "Other_Names";
                ddlClientIds.DataValueField = "ClientId";
                ddlClientIds.AppendDataBoundItems = true;
                ddlClientIds.DataBind();

                dlFuDividends.DataSource = DividendServices.GetFutureDividendReceivableCollection();
                dlFuDividends.DataBind();
               
                dlDividendRecs.DataSource = DividendServices.GetDividendReceivableCollection();
                dlDividendRecs.DataBind();

                dlSceipDividends.DataSource = DividendServices.GetScripDividendReceivableCollection();
                dlSceipDividends.DataBind();
            }
        }

        protected void btnApplyDividends_Click(object sender, EventArgs e)
        {
            DividendServices.AddDividendReceivables();
            Response.Redirect("~/Admin/DividendReceivables.aspx");           

        }

        protected void ddlClientIds_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlClientIds.SelectedValue != string.Empty)
            {
                dlFuDividends.DataSource = DividendServices.GetFutureDividReceivCollByClientId(ddlClientIds.SelectedValue);
                dlFuDividends.DataBind();

                dlDividendRecs.DataSource = DividendServices.GetDividendReceivCollByClientId(ddlClientIds.SelectedValue);
                dlDividendRecs.DataBind();

                dlSceipDividends.DataSource = DividendServices.GetScripDividendReceivableCollectionByClientId(ddlClientIds.SelectedValue);
                dlSceipDividends.DataBind();
            }
            else
            {
                dlFuDividends.DataSource = DividendServices.GetFutureDividendReceivableCollection();
                dlFuDividends.DataBind();

                dlDividendRecs.DataSource = DividendServices.GetDividendReceivableCollection();
                dlDividendRecs.DataBind();

                dlSceipDividends.DataSource = DividendServices.GetScripDividendReceivableCollection();
                dlSceipDividends.DataBind();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            foreach (DataListItem item in dlDividendRecs.Items)
            {
                Label lblClientId = (Label)item.FindControl("lblClientId");
                Label lblSecurityId = (Label)item.FindControl("lblSecurityId");
                Label lblXD = (Label)item.FindControl("lblXD");
                CheckBox chkReceived = (CheckBox)item.FindControl("chkReceived");                
                TextBox txbRemarks = (TextBox)item.FindControl("txbRemarks");

                DividendReceivable dr = new DividendReceivable();
                dr.ClientId = lblClientId.Text;
                dr.SecurityId = lblSecurityId.Text;
                dr.XD = DateTime.Parse(lblXD.Text);
                dr.Received = chkReceived.Checked;
                dr.Remarks = txbRemarks.Text;
                DividendServices.UpdateDividendReceivables(dr);                
            }

            Response.Redirect("~/Admin/DividendReceivables.aspx");

        }
    }
}
